// 请求
import axios from "axios"

function curring(first) {
    return function (operation) {
        if (operation === 'list') {
            return function (query) {
                // 获取题型列表时没有page选项 所以赋值空字符串
                let page = query ? `?pageNum=${query.pageNum}&pageSize=${query.pageSize}` : ''
                return `/${first}/${operation}${page}`
            }
        } else if (operation === 'login' || operation === 'add' || operation === 'all') {
            return `/${first}/${operation}`
        } else {
            return function (id) {
                return `/${first}/${operation}/${id}`
            }
        }
    }
}

/*
    请求结构：
        query: '路由携带的参数',
        method: "请求方式",
        url: "一级路由",
        route: "二级路由 操作方法",
        params: '请求体参数'
*/


// 获取列表数据
export const request = (data) => {
    // console.log(data, 'request');

    let firstCur = curring(data.url)

    let operationCur = firstCur(data.route)
    let url;
    if (data.query) {
        url = operationCur(data.query)
    } else if(data.route === 'list') {
        url = operationCur(data.query)
    }else{
        url = firstCur(data.route)
    }

    // console.log(url)

    return axios({
        method: data.method,
        url: url,
        data: data.params
    }).then(res => res.data).catch(err => { console.log(err); })
}